from PIL import Image  
  
# 打开灰度图片  
img = Image.open('棒棒糖.png').convert('L')  
  
# 获取图片的宽度和高度  
width, height = img.size  
  
# 遍历每一个像素  
for x in range(width):  
    for y in range(height):  
        # 获取当前像素的颜色值  
        color = img.getpixel((x, y))  
        with open("test2.txt","a+",encoding="utf-8") as f:
            f.write(str(color))
        # 输出像素位置和颜色值  
        #print('Pixel at ({}, {}): Color = {}'.format(x, y, color))
    with open("test.txt","a+",encoding="utf-8") as f:
        f.write("\n")